<?php
/*由脚本生成 powerBy--Reer*/

namespace app\common\manage;

use app\common\bean\StorePermissionBean;
use app\common\bean\StoreRolePermissionBean;
use app\common\exception\MyException;
use app\common\model\StorePermissionModel;
use app\common\model\StoreRolePermissionModel;
use app\common\validate\StorePermissionValidate;

class StorePermissionManage
{
    protected static $_self = null;
    static function getInstance(){
        if(empty(self::$_self)){
            self::$_self = new StorePermissionManage();
        }
        return self::$_self;
    }

    /** 添加权限节点
     * @param StorePermissionBean $storePermissionBean
     * @param bool $topPermission
     * @return bool
     * @throws \Exception
     */
    public function addPermissonNode(StorePermissionBean $storePermissionBean,$topPermission = false){
        if($topPermission){
            StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$addTop,$storePermissionBean->getData());
        }else{
            StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$addChild,$storePermissionBean->getData());
        }
        $result = StorePermissionModel::getInstance()->addDataGetInsertId($storePermissionBean->getData());
        $result ?:MyException::throwMyException("添加权限失败");
        return true;
    }

    /**
     * 根据id获取权限信息
     * @param StorePermissionBean $storePermissionBean
     * @return StorePermissionBean
     * @throws \Exception
     */
    public function getPermissionById(StorePermissionBean $storePermissionBean){
        StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$pkId,$storePermissionBean->getData());
        $result = StorePermissionModel::getInstance()->selectData(array(StorePermissionBean::$id => $storePermissionBean->getId()));
        $result ?:MyException::throwMyException("查询权限失败");
        $storePermissionBean->setData($result);
        return $storePermissionBean;
    }

    /** 获取权限的父级id
     * @param StorePermissionBean $storePermissionBean
     * @return StorePermissionBean
     * @throws \Exception
     */
    public function getParentPermission(StorePermissionBean $storePermissionBean){
        StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$pkId,$storePermissionBean->getData());
        $parentDetail = new StorePermissionBean();
        $parentDetail->setId($storePermissionBean->getParentId());
        try{
            $parentDetail = $this->getPermissionById($parentDetail);
        }catch (\Exception $exception){
            $parentDetail->setData(false);
        }
        return $parentDetail;
    }

    /** 修改权限
     * @param StorePermissionBean $storePermissionBean
     * @return bool
     * @throws \Exception
     */
    public function updatePermission(StorePermissionBean $storePermissionBean){
        if($storePermissionBean->getParentId()){
            StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$editTop,$storePermissionBean->getData());
        }else{
            StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$editChild,$storePermissionBean->getData());
        }
        $result = StorePermissionModel::getInstance()->updatedData(array(StorePermissionBean::$id => $storePermissionBean->getId()),$storePermissionBean->getData());
        $result ?:MyException::throwMyException("更新权限失败");
        return $result;
    }

    /** 删除权限
     * @param StorePermissionBean $storePermissionBean
     * @return int
     * @throws \Exception
     */
    public function delPermissionById(StorePermissionBean $storePermissionBean){
        StorePermissionValidate::getInstance()->goCheck(StorePermissionValidate::$pkId,$storePermissionBean->getData());
        $childCount = StorePermissionModel::getInstance()->getCount(array(StorePermissionBean::$parentId => $storePermissionBean->getId()));
        if($childCount === false || $childCount != 0){
            MyException::throwMyException("请先删除子权限/查询出错");
        }
        $result = StorePermissionModel::getInstance()->deleteData(array(StorePermissionBean::$id => $storePermissionBean->getId()));
        $result ?: MyException::throwMyException("删除出错");
        StoreRolePermissionModel::getInstance()->deleteData(array(StoreRolePermissionBean::$storePermissionId => $storePermissionBean->getId()));
        return $result;
    }
}